package ch09.game;

import java.io.*;

public class FileReaderManger {

    public static void save(Position position){
        try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("data/game3.save"))){
            String str = position.getX()+","+ position.getY();
            bufferedWriter.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Position load(){
        System.out.println("FileReaderManger...");
        Position position = new Position(0,0);
        try(BufferedReader bufferedReader = new BufferedReader(new FileReader("data/game3.save"))){
            String line = bufferedReader.readLine();
            String[] arr = line.split(",");
            int x = Integer.parseInt(arr[0]);
            int y = Integer.parseInt(arr[1]);
            position.setX(x);
            position.setY(y);
        } catch (FileNotFoundException e) {
            System.out.println("没有读取到文件，使用默认值");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return position;
    }
}
